<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta content="telephone=no" name="format-detection" />
    <!-- 留作将来统一添加 -->
    <TEMPLATE>${TEMPLATE}</TEMPLATE>
    <title>模板html</title>
    <link rel="stylesheet" type="text/css" href="../../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../../css/index.css">
    <link rel="stylesheet" type="text/css" href="../../css/data-flex.min.css">
</head>

<body>
    <header class="blue-bg" id="header" style="height: 0;min-height: 0;"></header>
    <footer class="blue-bg" id="footer" style="height: 0;min-height: 0;"></footer>
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/service/service-tool.js"></script>
<script type="text/javascript" src="../../script/service/service-app.js"></script>
<script type="text/javascript" src="../../script/service/service-user.js"></script>
<script type="text/javascript" src="../../script/service/service-logic.js"></script>
<script type="text/javascript" src="../../script/jquery.min.js"></script>
<script type="text/javascript" src="../../script/vue.min.js"></script>
<script type="text/javascript">
    // html 的引号一律为双引号(""),js 为单引号('')
    var USER, $S, vm;
    var headerH = 20, browser = null, footerH = 0;
    var showBrower = -1;
    var pageParam = {};
    apiready = function () {
        $S = SERVICE();
        USER = USER();
        console.log(JSON.stringify(api.pageParam) + '=====pageParam');
        pageParam = api.pageParam;
        browser = api.require('webBrowser');
        fnInit();
        api.addEventListener({
            name: 'keyback'
        }, function(ret, err) {
            openMainWin();
        });
        api.addEventListener({
            name: 'H5_gotoPay'
        }, function(ret) {
            var orderNo = ret.value.orderNo,
                id = ret.value.id,
                price = ret.value.price;
            console.log(orderNo + '==H5跳转支付,价格-》'+ price);
            fnOpenPublicWin('支付详情', 'video/pay',{
                retData: {
                    orderSn: orderNo,
                    orderTotalAmount: price,
                    id: id
                },
                from: 3,
                refreshWin: api.winName,
                refreshFrm: api.frameName
            });
        })
        api.addEventListener({
            name: 'hideExistFrm'
        }, function(ret) {
            // 隐藏
            console.log('hidden-exist_shop_frm')
            // api.setFrameAttr({
            //     name: 'exist_shop_frm',
            //     hidden: true
            // });
            api.closeFrame({
                name: 'exist_shop_frm'
            })
        });
        api.addEventListener({
            name: 'showExistFrm'
        }, function(ret) {
            // 展示
            console.log('show-exist_shop_frm')
            // api.setFrameAttr({
            //     name: 'exist_shop_frm',
            //     hidden: false
            // });
            addExistFrm();
        });
        api.addEventListener({
            name: 'loginStatus'
        }, function () {
            // 更新H5用户信息
            getMallToken();
        });
        api.addEventListener({
            name: 'shareGoods'
        }, function(ret) {
            // 分享商品
            var goodsId = ret.value.id;
            console.log('shareGoods：--'+goodsId);
            fnOpenFrame({
                name: 'share_frm',
                url: '../share.html',
                pageParam: {
                    businessId: goodsId,
                    shareGoods: 1
                }
            });
        });
        api.addEventListener({
            name: 'H5_gotoSelectAdress'
        }, function(ret) {
            // 选择地址
            var addressId = ret.value.id;
            console.log('选择地址:--'+addressId);
            var params = {
                name: 'h5_select_address_win',
                url: '../select_address_win.html',
                slidBackEnabled: false,
                animation: {
                    type: 'movein',
                    subType: 'from_bottom',
                    duration: 500
                },
                pageParam: {
                    winName: api.winName
                }
            };
            fnOpenWin(params);
        });
        // 打开商品详情
        api.addEventListener({
            name: 'shop_open_detail'
        }, function(ret) {
            var id = ret.value.id;
            openGoodsDetail(id);
        })
        // 再来一单
        api.addEventListener({
            name: 'shop_order_buyAgin'
        }, function(ret) {
            var submitPreview = ret.value.submitPreview;
            orderGotoBuy(submitPreview);
        });
        // banner打开商城
        api.addEventListener({
            name: 'fnBannerOpenShop'
        }, function(ret) {
            var bannerParams = ret.value.bannerParams;
            fnBannerOpenShop(bannerParams);
        });

        api.addEventListener({
            name: 'backToIndex'
        }, function() {
            fnSetShopOnIndex();
        })
    };

    // 获取商城token
    function getMallToken() {
        var params = {
            custId: USER.Get('custId', true)
        };
        if(!params.custId) {
            browser.loadScript({
                script: 'setUserInfo();'
            });
            return;
        }
        var callback = function(data) {
            var authorization = data.tokenHead + data.token;
            USER.Set({'authorization': authorization});
            var userType = getUserType();
            browser.loadScript({
                script: 'setUserInfo("'+ authorization +'",'+ userType +');'
            });
        };
        MALLPOST('single/home/getToken', params, callback);
    }

    // 初始化
    function fnInit() {
        vm = new Vue({
            el: '.vueApp',
            data: {
                info: null
            }
        });
        fixFooterBtn();
        headerH = AD('header').offsetH();
        footerH = AD('footer').offsetH();
        if(localStorage.loadShopUrl == '0') {
            openBrowser();
        }else {
            addExistFrm();
        }

        // api.addEventListener({
        //     name: 'shopFrameHidden'
        // }, function(ret) {
        //     var hidden = ret.value.hidden;
        //     if((showBrower == -1 || !showBrower) && !hidden) {
        //         // 从未打开过，且需要打卡
        //         // 已经关闭，且需要打开
        //         showBrower = true;
        //         openBrowser();
        //     }else if(showBrower && hidden) {
        //         // 已经打开，且需要关闭
        //         showBrower = false;
        //         browser.closeView();
        //     }
        // })
        // var params = {
        //     name: 'tab_shop_frm',
        //     url: 'http://52.80.15.168:8087/langwen',
        //     rect: {
        //         x: 0, y: headerH, w: 'auto', h: 'auto'
        //     },
        //     bgColor: '#5AC2FF',
        //     fixedOn: 'ui_window',
        //     progress: {
        //         type: 'page',
        //         color: '#FD9B43'
        //     }
        // }
        // fnOpenFrame(params);
    }

    function openBrowser() {
        var params = {
            // url: 'http://52.80.15.168:8087/langwen/#/pages/langwen/index',
            url: 'http://47.101.38.179:8080/langwen/#/pages/langwen/index',
            rect: {
                x: 0,
                y: headerH,
                w: api.winWidth,
                h: api.winHeight - headerH - footerH
            },
            progress: {
                color: '#FD9B43'
            }
        };
        browser.openView(params, function(ret, err) {
            switch (ret.state) {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    addExistFrm();
                    loadSuccess();
                    break;
                case 3:
                    break;
                case 4:
                    break;
                default:
                    break;
            }
        });
        setTimeout(function() {
            addExistFrm();
        }, 2*1000)
    }

    // 成功打开商城
    function loadSuccess() {
        if(localStorage.loadShopUrl != 1) {
            getMallToken();
        }
        localStorage.loadShopUrl = '1';
        setTimeout(function() {
            fnSetAppLabel();
            if(pageParam.goodsDetail) {
                openGoodsDetail(pageParam.goodsDetail);
            }
            if(pageParam.submitPreview) {
                orderGotoBuy(pageParam.submitPreview);
            }
            if(pageParam.bannerParams) {
                fnBannerOpenShop(pageParam.bannerParams);
            }
        }, 1000*0.5)
    }

    function addExistFrm() {
        var params = {
            name: 'exist_shop_frm',
            url: './exist_shop.html',
            rect: {
                x: api.winWidth -15 -64,
                y: headerH +10,
                w: '64', h: '24'
            }
        }
        fnOpenFrame(params);
    }

    // banner打开商城
    function fnBannerOpenShop(params) {
        // type 1 分类 2 商品 3 基本营销 4 满赠营销
        console.log(JSON.stringify(params));
        if(params.type == 1) {
            browser.loadScript({
                script: 'fnOpenGoodsCatagory("'+ params.id +'","'+ params.name +'");'
            });
        }else if(params.type == 2) {
            browser.loadScript({
                script: 'fnOpenGoodsDetail("'+ params.id +'");'
            });
        }else {
            browser.loadScript({
                script: 'fnOpenGoodsActivity("'+ params.id +'","'+ params.name +'","'+ params.type +'");'
            });
        }
        pageParam.bannerParams = '';
    }

    // 再来一单
    function orderGotoBuy(submitPreview) {
        submitPreview = JSON.stringify(submitPreview);
        browser.loadScript({
            script: "uni.setStorage({key: 'langwenabc', data: '"+ encodeURIComponent(submitPreview) +"'});"
        });
        browser.loadScript({
            script: 'fnOrderGotoBuy();'
        });
        pageParam.submitPreview = '';
        console.log("uni.setStorage({key: 'langwenabc', data: '"+ encodeURIComponent(submitPreview) +"'});");
    }

    // 选中地址
    function handleSelectAdress(data) {
        browser.loadScript({
            script: 'selectAddressCallback('+ JSON.stringify(data) +');'
        });
    }

    // 支付成功后的回调
    function paySuccessCallback() {
        browser.loadScript({
            script: 'paySuccessCallback();'
        });
    }

    // 设置APP标志
    function fnSetAppLabel() {
        browser.loadScript({
            script: 'fnSetAppLabel();'
        });
    }

    // 打开商品详情
    function openGoodsDetail(id) {
        console.error('打开商品详情==='+id);
        browser.loadScript({
            script: 'fnOpenGoodsDetail("'+ id +'","1");'
        });
        pageParam.goodsDetail = '';
    }

    // 商城设置在首页
    function fnSetShopOnIndex() {
        browser.loadScript({
            script: 'fnSetShopOnIndex();'
        });
    }
</script>

</html>